package com.majsubtense.core.components;

import javafx.scene.image.Image;

import java.io.InputStream;
import java.util.Optional;

public class Sprite extends Component {
    public static final ComponentType<Sprite> ANY;
    private Image image;

    public Sprite() {
        super(Sprite.ANY);
    }

    public void loadFromModule(String sourcePath) {
        Optional<Module> specificModule = ModuleLayer.boot().findModule(getClass().getModule().getName());
        specificModule.ifPresent(module -> {
            try {
                InputStream is = module.getResourceAsStream(sourcePath);
                this.image = new Image(is);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });
    }

    public void load(String sourcePath) {
        this.image = new Image(sourcePath);
    }

    public Image getImage() {
        return image;
    }

    static {
        ANY = new ComponentType<>(ComponentType.ROOT, "SPRITE");
    }
}
